﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Reconciliation.Data
{
    public partial class OrderCycle
    {
        public static Domain.OrderCycle MapDataToDomain(Data.OrderCycle dataOrderCycle)
        {
            Domain.OrderCycle domainOrderCycle = new Reconciliation.Domain.OrderCycle();
            Domain.OrderCycleEmployee domainOrderCycleEmployee = null;
            domainOrderCycle.DateTime =  dataOrderCycle.DateTime ;
            domainOrderCycle.OrderCycleID =  dataOrderCycle.OrderCycleID ;
            domainOrderCycle.Status =  dataOrderCycle.Status;
            domainOrderCycle.OrderID = dataOrderCycle.Order.OrderID;
            foreach (Data.OrderCycleEmployee oce in dataOrderCycle.OrderCycleEmployee)
            {
                domainOrderCycleEmployee = Data.OrderCycleEmployee.MapDataToDomain(oce);
                domainOrderCycle.OrderCycleEmployeeList.Add(domainOrderCycleEmployee);
            }
            return domainOrderCycle;
        }

        public static List<Domain.OrderCycle> GetOrderCycle(int orderID)
        {
            try
            {
                List<Domain.OrderCycle> domainOrderCycleList = new List<Reconciliation.Domain.OrderCycle>();
                Domain.OrderCycle domainOrderCycle = null;
                ReconciliationEntities recEnt = new ReconciliationEntities();
                List<Data.OrderCycle> dataOrderCycleList = recEnt.OrderCycle.Include("OrderCycleEmployee").Include("Order").Where(oc => oc.Order.OrderID.Equals(orderID)).ToList();
                for (int i = 0; i < dataOrderCycleList.Count; i++)
                {
                    domainOrderCycle = OrderCycle.MapDataToDomain(dataOrderCycleList[i]);
                    domainOrderCycleList.Add(domainOrderCycle);
                }
                return domainOrderCycleList;
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, "Exception Policy");
                throw new Domain.ReconciliationException("Database error has occured please try again.");
            }
        }
    }
}
